package com.xwj.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.xwj.Enums.StatusInfoEnum;
import com.xwj.dto.RepairInfoDto;
import com.xwj.entity.RaRepairInfo;
import com.xwj.service.Impl.RaRepairInfoServiceImpl;
import com.xwj.service.Impl.RaStatusInfoServiceImpl;
import com.xwj.service.holder.RaRegisterUserHolder;
import com.xwj.util.ValidatedUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/repairInfo/")
public class RepairController {

    @Autowired
    private RaRepairInfoServiceImpl raRepairInfoService;
    @Autowired
    private RaStatusInfoServiceImpl raStatusInfoService;

    @RequestMapping(value = "repairInfoList")
    public ModelAndView repairInfoList(ModelAndView modelAndView) {
        String label = StatusInfoEnum.RepirInfo.getName();
        modelAndView.addObject("repairStatus", raStatusInfoService.findStatusByLabel(label));
        modelAndView.setViewName("/admin/repair/repairInfo");
        return modelAndView;
    }

    /**
     * 显示或查询
     *
     * @param pageIndex
     * @param pageSize
     * @param key
     * @return
     */
    @RequestMapping(value = "houseInfoDetail", method = RequestMethod.POST)
    public Map<String, Object> repariInfoList(@RequestParam(value = "page") Integer pageIndex,
                                              @RequestParam(value = "limit") Integer pageSize,
                                              @RequestParam(value = "key", required = false) String key) {
        PageInfo<RaRepairInfo> page;
        RepairInfoDto repairInfoDto = null;
        if (key != null) repairInfoDto = JSON.parseObject(key, new TypeReference<RepairInfoDto>() {
        });
        if (repairInfoDto == null)
            page = raRepairInfoService.findAllInfo(pageIndex, pageSize);
        else
            page = raRepairInfoService.findByProperty(pageIndex, pageSize, repairInfoDto);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", page.getTotal());
        map.put("data", page.getList());
        return map;
    }

    /**
     * 添加维修信息
     *
     * @param repairInfoDto
     * @return
     */
    @RequestMapping(value = "addOneRepair", method = RequestMethod.POST)
    public Map<String, Object> addOneRepair(RepairInfoDto repairInfoDto) {
        Map<String, Object> map = new HashMap<>();
        RaRepairInfo raRepairInfo = new RaRepairInfo(repairInfoDto);
        if (!RaRegisterUserHolder.hasUser(repairInfoDto.getUserId())) {
            map.put("msg", "无此用户，请重新输入");
            return map;
        }
        if (!ValidatedUtils.checkCellphone(repairInfoDto.getPhone())) {
            map.put("msg", "请输入正确的联系电话");
            return map;
        }
        if (raRepairInfoService.addOneInfo(raRepairInfo) > 0)
            map.put("msg", "添加成功");
        else
            map.put("msg", "添加失败");
        return map;
    }

    /**
     * 更新
     *
     * @param id
     * @param status
     * @param title
     * @param reason
     * @param phone
     * @param address
     * @return
     */
    @RequestMapping(value = "updateRepairInfo", method = RequestMethod.POST)
    public Map<String, Object> updateRepairInfo(String id,
                                                @RequestParam(value = "userId", required = false) String userId,
                                                @RequestParam(value = "status", required = false) Integer status,
                                                @RequestParam(value = "title", required = false) String title,
                                                @RequestParam(value = "reason", required = false) String reason,
                                                @RequestParam(value = "phone", required = false) String phone,
                                                @RequestParam(value = "address", required = false) String address) {
        Map<String, Object> map = new HashMap<>();
        if (!RaRegisterUserHolder.hasUser(userId)) {
            map.put("msg", "无此用户，请重新输入");
            return map;
        }
        if (!ValidatedUtils.checkCellphone(phone)) {
            map.put("msg", "请输入正确的联系电话");
            return map;
        }
        if (raRepairInfoService.updateByProperty(id, userId, status, title, reason, phone, address) > 0)
            map.put("msg", "更新成功");
        else
            map.put("msg", "更新失败");
        return map;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteById", method = RequestMethod.POST)
    public Map<String, Object> deleteById(@RequestParam(value = "id") String id) {
        Map<String, Object> map = new HashMap<>();
        List<String> ids = new ArrayList<>();
        ids.add(id);
        if (raRepairInfoService.deleteByIds(ids) > 0)
            map.put("msg", "删除成功");
        else
            map.put("msg", "数据有误，删除失败");
        return map;
    }

    /**
     * 批量删除
     *
     * @param mydata
     * @return
     */
    @RequestMapping(value = "deleteBatchById", method = RequestMethod.POST)
    public Map<String, Object> deleteBatchById(@RequestParam(value = "mydata", required = false) String mydata) {
        Map<String, Object> map = new HashMap<>();
        List<RaRepairInfo> raRepairInfoList = JSON.parseArray(mydata, RaRepairInfo.class);
        if (raRepairInfoList.size() == 0) {
            map.put("msg", "未选中任何选项");
            return map;
        }
        List<String> ids = raRepairInfoService.parseListId(raRepairInfoList);
        if (raRepairInfoService.deleteByIds(ids) > 0)
            map.put("msg", "删除成功");
        else
            map.put("msg", "数据操作异常");
        return map;
    }

}
